﻿#region  文件信息

// ---------------------------------------------------------------------------------------
// <copyright file="WxResponseJsApi.cs" company="">
// 作者：李帅伦
// </copyright>
// <summary>
// 类型简介：
// 类型详细：WxResponseJsApi.cs
// 创建时间：2014-12-25 12:58
// 修改时间：2014-12-22 17:43
// </summary>
// ---------------------------------------------------------------------------------------
#endregion

using WebCommon.Com;

namespace Payment.Weixin
{
    #region

    using System;
    using System.Collections.Generic;

    using Newtonsoft.Json;

    using WebCommon;

    #endregion

    /// <summary>
    /// The wx response js api.
    /// </summary>
    [JsonObject]
    public class WxResponseJsApi
    {
        #region Fields

        /// <summary>
        /// The wx config.
        /// </summary>
        [JsonIgnore]
        protected WxConfig wxConfig;

        /// <summary>
        /// The wx package.
        /// </summary>
        [JsonIgnore]
        protected WxPackage wxPackage;

        /// <summary>
        /// The pay Service.
        /// </summary>
        [JsonIgnore]
        protected IPayOrder PayOrder;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="WxResponseJsApi"/> class.
        /// </summary>
        /// <param name="wxConfig">
        /// The wx config.
        /// </param>
        /// <param name="wxPackage">
        /// The wx package.
        /// </param>
        public WxResponseJsApi(WxConfig wxConfig, WxPackage wxPackage)
        {
            this.wxConfig = wxConfig;
            this.AppId = wxConfig.AppId;
            this.PayOrder = wxPackage.PayOrder;
            this.wxPackage = wxPackage;
            this.NonceStr = new RandomsAzNum(16).OutputAzLower();
            this.TimeStamp = ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString();
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the app id.
        /// </summary>
        [JsonProperty("appid")]
        public string AppId { get; protected set; }

        /// <summary>
        /// Gets or sets the nonce str.
        /// </summary>
        [JsonProperty("nonceStr")]
        public string NonceStr { get; set; }


        /// <summary>
        /// Gets or sets the package.
        /// </summary>
        [JsonProperty("package")]
        public string Package { get; protected set; }

        /// <summary>
        /// Gets or sets the pay sign.
        /// </summary>
        [JsonProperty("paySign")]
        public string PaySign { get; protected set; }

        /// <summary>
        /// Gets the sign type.
        /// </summary>
        [JsonProperty("signType")]
        public string SignType
        {
            get
            {
                return "SHA1";
            }
        }

        /// <summary>
        /// Gets or sets the time stamp.
        /// </summary>
        [JsonProperty("timeStamp")]
        public string TimeStamp { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The fasong.
        /// </summary>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public string Fasong()
        {
            this.Package = this.wxPackage.GetPackage();
            this.PaySign = this.GetPaySing();
            return JsonConvert.SerializeObject(this);
        }

        /// <summary>
        /// 保存参数
        /// </summary>
        /// <param name="path">
        /// </param>
        public void Save(string path)
        {
            // var fp = "微信签名后参数：" + this.PayReturn;
            // File.WriteAllText(path, fp);
        }

        /// <summary>
        /// 进行验签
        /// </summary> 
        /// <returns></returns>
        public string GetPaySing()
        {
            var dic = new Dictionary<string, string>();
            dic.Add("appid", this.AppId);
            dic.Add("appkey", this.wxConfig.Appkey);
            dic.Add("noncestr", this.NonceStr);
            dic.Add("timestamp", this.TimeStamp);
            dic.Add("package", this.Package ?? this.wxPackage.GetPackage());

            string orderBy = WxCommon.GetOrderBy(dic);

            return Encryption.Sha1(orderBy);
        }
        #endregion
    }
}